package com.ruoyi.module.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.module.mapper.MaterialRecordMapper;
import com.ruoyi.module.domain.MaterialRecord;
import com.ruoyi.module.service.IMaterialRecordService;

/**
 * 课程学习记录Service业务层处理
 *
 * @author fubojin
 * @date 2025-03-05
 */
@Service
public class MaterialRecordServiceImpl implements IMaterialRecordService {
    @Autowired
    private MaterialRecordMapper materialRecordMapper;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询课程学习记录
     *
     * @param id 课程学习记录主键
     * @return 课程学习记录
     */
    @Override
    public MaterialRecord selectMaterialRecordById(Long id) {
        return materialRecordMapper.selectMaterialRecordById(id);
    }

    /**
     * 查询课程学习记录列表
     *
     * @param materialRecord 课程学习记录
     * @return 课程学习记录
     */
    @Override
    public List<MaterialRecord> selectMaterialRecordList(MaterialRecord materialRecord) {
        List<MaterialRecord> materialRecords = materialRecordMapper.selectMaterialRecordList(materialRecord);
        for (MaterialRecord record : materialRecords) {
            record.setUser(userService.selectUserById(record.getUserId()));
        }
        return materialRecords;
    }

    /**
     * 新增课程学习记录
     *
     * @param materialRecord 课程学习记录
     * @return 结果
     */
    @Override
    public int insertMaterialRecord(MaterialRecord materialRecord) {
        MaterialRecord materialRecord1 = materialRecordMapper.selectMaterialRecordByUserId(materialRecord.getUserId());
        if (materialRecord1 == null) {

            materialRecord.setCreateTime(DateUtils.getNowDate());
            if (materialRecord.getLearningFrequency() == null) {
                materialRecord.setLearningFrequency(0);
            }
            if (materialRecord.getDuration() == null) {
                materialRecord.setDuration(0.00);
            }
            return materialRecordMapper.insertMaterialRecord(materialRecord);

        } else {
            materialRecord1.setDuration(materialRecord.getDuration() == null ? materialRecord1.getDuration() : materialRecord.getDuration() + materialRecord1.getDuration());
            return materialRecordMapper.updateMaterialRecord(materialRecord1);
        }


    }

    /**
     * 修改课程学习记录
     *
     * @param materialRecord 课程学习记录
     * @return 结果
     */
    @Override
    public int updateMaterialRecord(MaterialRecord materialRecord) {
        materialRecord.setUpdateTime(DateUtils.getNowDate());
        return materialRecordMapper.updateMaterialRecord(materialRecord);
    }

    /**
     * 批量删除课程学习记录
     *
     * @param ids 需要删除的课程学习记录主键
     * @return 结果
     */
    @Override
    public int deleteMaterialRecordByIds(Long[] ids) {
        return materialRecordMapper.deleteMaterialRecordByIds(ids);
    }

    /**
     * 删除课程学习记录信息
     *
     * @param id 课程学习记录主键
     * @return 结果
     */
    @Override
    public int deleteMaterialRecordById(Long id) {
        return materialRecordMapper.deleteMaterialRecordById(id);
    }

    @Override
    public MaterialRecord selectMaterialRecordByUserId(Long userId) {

        MaterialRecord materialRecord = materialRecordMapper.selectMaterialRecordByUserId(userId);
        if (materialRecord != null) {
            List<MaterialRecord> list = materialRecordMapper.selectMaterialRecordList(new MaterialRecord());
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getId().equals(materialRecord.getId())){
                    materialRecord.setLearningRanking(i+1);
                }
            }
        }
        return materialRecord;
    }
}
